<template>
	<div class="system-dept-container layout-padding">
		<div class="layout-padding-auto layout-pd card-bg energy-table current-page">
			<div class="system-dept-search">
				<!-- <span>部门名称 : </span>
				<el-input v-model="state.formData.deptName" size="default" clearable placeholder="请输入部门名称" style="max-width: 180px"> </el-input>
				<span style="margin-left: 50px">部门状态 : </span>
				<el-select v-model="state.formData.status" placeholder="请选择部门状态" size="default">
					<el-option v-for="item in state.options" :key="item.value" :label="item.label" :value="item.value" />
				</el-select> -->
				<!-- <el-button size="default" type="primary" class="ml10" @click="getTableData()">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button> -->
				<!-- <el-button size="default" class="ml10" @click="refreshData">
					<el-icon>
						<ele-Refresh />
					</el-icon>
					重置
				</el-button> -->
				<el-form-item>
					<div class="btn1" @click="onOpenAddDept('add', '')">
						<el-icon> <ele-CirclePlus /> </el-icon><span class="ml6">新增部门</span>
					</div>
				</el-form-item>

				<!-- <el-button size="default" type="success" class="ml10" @click="onOpenAddDept('add', '')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增部门
				</el-button> -->
			</div>
			<el-table
				v-loading="state.tableData.loading"
				:data="state.tableData.data"
				style="width: 100%"
				row-key="id"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				:row-style="{ background: '#0F2C6A' }"
				:header-cell-style="{ background: '#1E4897', color: '#499AFD' }"
			>
				<el-table-column prop="deptName" label="部门名称" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="status" label="部门状态" show-overflow-tooltip>
					<template #default="scope">
						<div :class="scope.row.status == 1 ? 'energy-tag1' : 'energy-tag2'">
							<el-tag size="default">{{ scope.row.status == 1 ? '禁用' : '正常' }}</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="200" fixed="right">
					<template #default="scope">
						<el-button size="small" link type="success" @click="onOpenAddDept('add', scope.row)">新增</el-button>
						<el-button size="small" link type="primary" @click="onOpenEditDept('edit', scope.row)">修改</el-button>
						<el-button size="small" link type="danger" @click="delTableRow(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<DeptDialog ref="deptDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemDept">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDeptApi } from '/@/api/system/dept/index';

// 引入组件
const DeptDialog = defineAsyncComponent(() => import('/@/views/system/dept/dialog.vue'));

// 定义变量内容
const deptDialogRef = ref();
const state = reactive<any>({
	options: [
		{ label: '正常', value: 0 },
		{ label: '停用', value: 1 },
	],
	formData: {
		deptName: '',
		status: '', // 部门状态:0正常,1停用
	},
	tableData: {
		data: [],
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	const { data } = await useDeptApi().getListDept(state.formData);
	state.tableData.data = data;
	state.tableData.loading = false;
	setTimeout(() => {
		state.tableData.loading = false;
	}, 2000);
};

// 重置
// const refreshData = () => {
// 	state.formData = {
// 		name: '', // 岗位名称
// 		state: '', // 岗位状态
// 	};
// 	getTableData();
// };

// 打开新增弹窗
const onOpenAddDept = (type: string, row: any) => {
	deptDialogRef.value.openDialog(type, row);
};

// 打开编辑弹窗
const onOpenEditDept = (type: string, row: any) => {
	deptDialogRef.value.openDialog(type, row);
};

// 删除当前行
const delTableRow = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除部门：${row.deptName}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await useDeptApi().delDeptById(row.id);
			getTableData();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
